home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.onyx.net!claymoor
- From: Adam.Morris@octacon.co.uk (Adam Morris)
- Newsgroups: comp.lang.c++
- Subject: Re: A simple "find the bug" (please! I need help :)
- Date: Wed, 28 Feb 96 14:52:28 GMT
- Organization: Octacon Ltd
- Message-ID: <4h18m5$q4a@mulgave.octacon.co.uk>
- References: <4gdr6n$6p0@guava.epix.net>
- NNTP-Posting-Host: claymoor.onyx.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4gdr6n$6p0@guava.epix.net>, jgvd@epix.net (Jon) wrote:
- >I wrote this just now and it's telling me that when i try to cout the
- >result at the bottom, it is an undefined value even though I did return
- >the result var... See if you can help.
-
- o.k. I've removed the quoted bits from the program so that it looks o.k.
-
-
- // --- Program starts here
- // include input and output stream stuff
- #include <iostream.h>
-
- // declare two GLOBAL (these should really be passed in as parameters)
- // variables
- float num1;
- float num2;
-
- // function getnums -- this has no return type, usually that means it will
- // default to int... we want float so make it so:-
-
- float // remove this comment, but this is the
- return type
- getnums () {
-
- // declare a float called result and give it value num1+num2.
- // this is a local variable and will vanish when this function ends
- float result = num1 +num2;
-
- // now we get some numbers in...
- cout << "What number?\n";
- cin >> num1;
- cout << "And?\n";
- cin >> num2;
-
- // return the value of num1+num2, FROM BEFORE WE GOT THEM IN!!!
- return result;
- }
-
- main (){
-
- getnums (); // We are ignoring the return value, we should put it into a
- // variable
-
- cout << result;
- }
-
- > ^^^^^^ right there is the result i want printed but it does not
- >recognize it even though I did try to return the value in the function
- >getnums... am I missing something?
-
- A few things... try this instead
- // --- Program starts here
- // include input and output stream stuff
- #include <iostream.h>
- float
- getnums()
- {
- // declare two local variables to hold our input numbers
- float theFirstNumber;
- float theSecondNumber;
-
- // now we get some numbers in...
- cout << "What number?" << endl; // let's use C++ stream end lines...
- cin >> theFirstNumber;
- cout << "And?" << endl;
- cin >> theSecondNumber;
-
- // now we create another local variable to hold the result
- float theResult = theFirstNumber + theSecondNumber;
- //and send it back to the calling function
- return theResult;
- // not this last bit is inefficient, we could have just used
- // return(theFirstNumber + theSecondNumber);
- }
-
- main ()
- {
- float aResult= getnums(); // create a local variable to hold the result of
- // our call to getnums Notice that this doesn't
- // need to have the same name as the returned
- // variable in the getnums() function
-
- cout << aResult; // output it.
- }
-
- Adam
-
- P.S. any typos in this are all my fault, I haven't compiled it, and I typed
- it into my newsreader...
-
-
-
-